package com.study.chapter05.operator.sink;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;

import java.util.Properties;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/7/30 16:04
 */
public class Sink2KafkaTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.30.100:9092");

        //从文件中读取数据
        DataStreamSource<String> stream = env.readTextFile("input/cart.txt");

        //将数据写入到kafka中
        stream.addSink(new FlinkKafkaProducer<String>(
                "clicks",
                new SimpleStringSchema(),
                properties
        ));
        //之后直接在服务器上启动一个kafka的消费者，查看是否接收到数据
        //./kafka-console-consumer --bootstrap-server 192.168.30.100:9092 --topic clicks

        env.execute();
    }
}
